#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
    struct Data {
        int id;
        vector<int> ranks;
        bool flag = false;
        Data(int id_,int size) :id(id_), ranks(size,0){
        }
    };
public:
    string rankTeams(vector<string>& votes) {
        if (votes.size() == 1) return votes[0];
        vector<Data> ranks;
        for (int i = 0; i < 26; ++i) {
            ranks.emplace_back(i,votes[0].size());
        }
        for (int i = 0; i < votes.size(); ++i) {
            for (int j = 0; j < votes[i].size(); ++j) {
                ranks[votes[i][j] - 'A'].ranks[j]++;
                ranks[votes[i][j] - 'A'].flag = true;
            }
        }
        sort(ranks.begin(), ranks.end(), [](const Data& left, const Data& right) {
            for (int i = 0; i < left.ranks.size(); ++i) {
                if (left.ranks[i] > right.ranks[i])
                    return true;
                if (left.ranks[i] < right.ranks[i])
                    return false;
            }
            return left.id < right.id;

            });
        string res = "";
        for (int i = 0; i < ranks.size(); ++i) {
            if (!ranks[i].flag) break;
            res += ranks[i].id + 'A';
        }
        return res;
    }
};